Find and Replace

Hello,

I have a string array which is like { "Apple", "Banana", "Man go"} etc.
I am trying to get a script to weed out un-needed characters out of my text like any extra spaces carriage returns etc.
I wrote the script to pass the string array into the function and then do the activity but it is giving me a very simple looking error which I am not able to remove.

bool RemoveSpaces(string data50, int arrayLen)
{
Buffer buf = create;
int cnt = 0, cnt1 = 0

for(cnt=0; cnt<arrayLen; cnt++)
{
for(cnt1=0; cnt1<length(datacnt); cnt1++)
{
char check = datacntcnt1:cnt1 //**Seeing the Error Here**

if ((check != charOf(32)) && (check != charOf(10)) &&
(check != charOf(13)) && (check != charOf(9)))
{
buf+= datacnt1
}
}
datacnt = stringOf(buf)
}
}

The error seen is:

-E- DXL: <Line:10> incorrect arguments for (=)
-I- DXL: All done. Errors reported: 1. Warnings reported: 0.

Any help??
Thanks
jnbharadhwaj - Tue Jun 26 03:57:22 EDT 2012

Re: Find and Replace
Peter_Albert - Tue Jun 26 06:02:44 EDT 2012

First of all, when posting DXL code, please put "{ c o d e }" before and after the actual code (remove the spaces). This will format the code nicely, and, more important in your specific case, will preserve the angular brackets, which have been removed and replaced by hyperlinks. I can therefore only guess what your original code in line 10 was, I guess something like

char check = data[cnt][cnt1:cnt1]

.The specific error in this line is that 'char' assignment is done by individual integers, not ranges, hence

data[cnt][cnt1]

works.

I have a few more suggestions:

'data' as a variable name is not a good idea, as it is also the name of a DXL routine, I propose 'indata' or something like that.

You have to delete the Buffer to avoid memory leaks

You don't have to pass the length of the array ('arrayLen'), usahe of 'sizeof indata' does the job

You have to empty the buffer in each outer loop, otherwise your result becomes {"Apple","AppleBanana","AppleBananaMango"}

You either have to return a return value (you defined 'RemoveSpaces' to return a booelan variable), or change it to 'void'

The complete code with all suggested modifications looks like this:

void RemoveSpaces(string indata[])
{ 
  Buffer buf = create
  int    cnt = 0, cnt1 = 0
  int    arrayLen = sizeof indata
  char   check
  for(cnt=0; cnt<arrayLen; cnt++)
  {
    buf = ""   // Empty the buffer for each array element
    for(cnt1=0; cnt1<length(indata[cnt]); cnt1++)
    {
      check = indata[cnt][cnt1]
      if ((check != charOf(32)) && (check != charOf(10)) && 
        (check != charOf(13)) && (check != charOf(9)))
      {
        buf+= check
      }
    }
    indata[cnt] = stringOf(buf)
  }
  delete buf   // Don't forget to delete the Buffer
}
string sTmp[] = {"Apple", "Banana", "Man go"}
int i
for i in 0:sizeof sTmp -1 do {print sTmp[i] ","}
print "\n"
RemoveSpaces(sTmp)
for i in 0:sizeof sTmp -1 do {print sTmp[i] ","}

 


Cheers,

Peter

 

Re: Find and Replace
llandale - Tue Jun 26 16:30:23 EDT 2012

Using objective investigation and subjective judgement, I've come up with the following variable that defines each "White Space" character. My "Trim" etc functions use this definition:

  • const string cl_White = " \t\n\v\f\r" charOf(160) ""

If you use that, perhaps you need an inner loop:

bool HasWhite = false
for (j = 0; j < length(cl_White); j++)
{  if (check == cl_White[j]) then HasWhite = true
}
if (!HasWhite) then buf += check


-Louie

Re: Find and Replace
Mathias Mamsch - Wed Jun 27 05:14:35 EDT 2012

llandale - Tue Jun 26 16:30:23 EDT 2012

Using objective investigation and subjective judgement, I've come up with the following variable that defines each "White Space" character. My "Trim" etc functions use this definition:

  • const string cl_White = " \t\n\v\f\r" charOf(160) ""

If you use that, perhaps you need an inner loop:

bool HasWhite = false
for (j = 0; j < length(cl_White); j++)
{  if (check == cl_White[j]) then HasWhite = true
}
if (!HasWhite) then buf += check


-Louie

Just one nit-pick regarding those loops. One should not use 'length' in the loop condition. The loop condition will be executed in every iteration of the loop. Note that this only applies to strings, not to buffers (which keep their length stored, as long as you do not modify them). This can lead to hard to find performance killers in DXL, because a loop that is supposed to be checking for whitespaces suddenly takes 1000% more time than before (when only small strings were used).

Regards, Mathias
 

pragma runLim, 0
int i = 0
Buffer bufLong = create(); 
// create a long string
for (i = 0; i < 20000; i++) { bufLong += 'x' }
 
// Buffer buf = bufLong // comment me in to test with buffers
string buf = stringOf bufLong  
 
{ 
   int iStart = getTickCount_() 
   // use length in loop condition 
   for (i = 0; i < length buf; i++) { }
   print "Time with length in loop:" (getTickCount_() - iStart) "\n"
}
 
{ 
   int iStart = getTickCount_() 
   // use length in loop condition 
   int iLen = length buf; 
   for (i = 0; i < iLen; i++) { }
   print "Time with length outside loop:" (getTickCount_() - iStart) "\n"
}

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Re: Find and Replace
llandale - Wed Jun 27 15:47:28 EDT 2012

Mathias Mamsch - Wed Jun 27 05:14:35 EDT 2012

Just one nit-pick regarding those loops. One should not use 'length' in the loop condition. The loop condition will be executed in every iteration of the loop. Note that this only applies to strings, not to buffers (which keep their length stored, as long as you do not modify them). This can lead to hard to find performance killers in DXL, because a loop that is supposed to be checking for whitespaces suddenly takes 1000% more time than before (when only small strings were used).

Regards, Mathias
 

pragma runLim, 0
int i = 0
Buffer bufLong = create(); 
// create a long string
for (i = 0; i < 20000; i++) { bufLong += 'x' }
 
// Buffer buf = bufLong // comment me in to test with buffers
string buf = stringOf bufLong  
 
{ 
   int iStart = getTickCount_() 
   // use length in loop condition 
   for (i = 0; i < length buf; i++) { }
   print "Time with length in loop:" (getTickCount_() - iStart) "\n"
}
 
{ 
   int iStart = getTickCount_() 
   // use length in loop condition 
   int iLen = length buf; 
   for (i = 0; i < iLen; i++) { }
   print "Time with length outside loop:" (getTickCount_() - iStart) "\n"
}

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

thanks.